package com.csap.framework.oauth2.server.service.impl;

import com.csap.framework.annotation.apidoc.Description;
import com.csap.framework.oauth2.server.entity.OtherOpenEntity;
import com.csap.framework.oauth2.server.model.OtherOpenModel;
import com.csap.framework.oauth2.server.mybatis.OtherOpenMapper;
import com.csap.framework.oauth2.server.service.OtherOpenService;
import com.csap.framework.oauth2.type.OtherOpenType;
import com.csap.mybatisplus.core.toolkit.IdWorker;
import com.csap.mybatisplus.extension.service.impl.BaseCsapServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 其他第三方open信息
 * </p>
 *
 * @author yangchengfu
 * @dateTime 2021-08-09 14:41:34
 */
@Service
@Description(value = "其他第三方open信息ServiceImpl")
public class OtherOpenServiceImpl extends BaseCsapServiceImpl<OtherOpenMapper, OtherOpenEntity, OtherOpenModel, Long> implements OtherOpenService {

    @Override
    public String openId(String openId, String userId, OtherOpenType type) {
        return lambdaQuery()
                .select(OtherOpenEntity::getOpenId, OtherOpenEntity::getId)
                .eq(OtherOpenEntity::getOpenId, openId)
                .eq(OtherOpenEntity::getUserId, userId)
                .eq(OtherOpenEntity::getType, type)
                .oneOptional()
                .transformOptional(OtherOpenModel.class)
                .isNull(() -> OtherOpenModel.builder()
                        .openId(openId)
                        .type(type)
                        .userId(userId)
                        .build())
                .when(i -> i.getId() == null, this::insert)
                .value(OtherOpenModel::getOpenId);
    }

}
